Thread: First time using pointers, and i feel lost :|

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    11

    First time using pointers, and i feel lost :|

    It's me again, trying to figure out how to use pointers,
    Im supposed to calculate alpha, beta and the lenght of c of a trianlge using a function (triangle_1)
    and from what i think i'll have to use *pointers to get the values out of the function.
    i tested the *cc, *alpha and *beta calculations outside a function and they seem to work

    Code:
    #include <stdio.h>#include <math.h>
    #define TOTAL_DEG  (180.0)
    
    
    void triangle_1 (double a, double b,double c, double gamma, double *cc, double *alpha, double *beta)
        {
            
            *cc= sqrt((a*a+b*b-(2*a*b*cos(gamma))));
            *alpha =(gamma)*(acos((double)(b*b +(c)*(c)- a*a)/(2.0*b*c)));
            *beta = -gamma-*alpha+TOTAL_DEG;
            
        }    
            
            
    int main()
    {
        double a,b,c,gamma,alpha,beta,result;
        double tri;
        printf("Bitte gib a, b und gamma eines Dreiecks ein\n");
        scanf("%lf,%lf,%lf",&a,&b,&gamma);
        prinf("a,b,c: %lf %lf %lf; gegenueber liegende Winkel: %lf %lf %lf",a,b,c,*alpha,*beta,gamma);
        
        return 0;
        
        
    }
        
        
    }

    thanks for the help o/


    *edit, im unsure if im missing these to get the rad to degree or the other way around, i always hated trigonometry
    double rad (double grad) {return (grad/45.) * atan(1.);}
    double grad (double rad) {return (rad*45.) / atan(1.);}
    Last edited by Lyrockzz; 11-21-2018 at 06:17 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that given a double variable named alpha, to get a pointer to alpha, you should use the address-of operator &, not the dereference operator, i.e., &alpha

    By the way, triangle_1 is a rather poor name for a function. You probably have no choice in the matter, but it would be better design to have three functions:
    Code:
    double compute_length_of_third_side(double a, double b, double gamma)
    {
        return sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
    }
    
    double compute_alpha(double a, double b, double c, double gamma)
    {
        return gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));
    }
    
    double compute_beta(double gamma, double alpha)
    {
        return -gamma - alpha + TOTAL_DEG;
    }
    Notice that this reveals a problem with your triangle_1 function: you don't need c because you're computing it, and subsequently your use of c without overwriting it is a bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Recall that given a double variable named alpha, to get a pointer to alpha, you should use the address-of operator &, not the dereference operator, i.e., &alpha

    By the way, triangle_1 is a rather poor name for a function. You probably have no choice in the matter, but it would be better design to have three functions:
    Code:
    double compute_length_of_third_side(double a, double b, double gamma)
    {
        return sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
    }
    
    double compute_alpha(double a, double b, double c, double gamma)
    {
        return gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));
    }
    
    double compute_beta(double gamma, double alpha)
    {
        return -gamma - alpha + TOTAL_DEG;
    }
    Notice that this reveals a problem with your triangle_1 function: you don't need c because you're computing it, and subsequently your use of c without overwriting it is a bug.
    ye the guideline says "Write a Function (triangle_1) that computes those calculations (prototype is given) void triangle_1 (double a, double b, double gamma, double *c, double *alpha,double *beta);
    thought about using multiple functions aswell :/

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    11
    Code:
    #include <stdio.h>#include <math.h>
    #include <stdlib.h>
    #define TOTAL_DEG  (180.0)
    
    
    double berechnung_length(double a, double b, double gamma, double c)
    {	
        c = sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
    	return c;
    }
     
    double berechnung_alpha(double a, double b, double c, double gamma, double alpha)
    {
    	alpha = gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));
        return alpha;
    }
     
    double berechnung_beta(double gamma, double alpha, double beta)
    {
    	beta = - gamma - alpha + TOTAL_DEG;
         return beta;
    }
    
    
    //void triangle_1 (double a, double b,double c, double gamma, double *cc, double *alpha, double *beta)
    //	{
    //		
    //		*cc= sqrt((a*a+b*b-(2*a*b*cos(gamma))));
    //		*alpha =(gamma)*(acos((double)(b*b +(c)*(c)- a*a)/(2.0*b*c)));
    //		*beta = -gamma-*alpha+TOTAL_DEG;
    //		
    //	}	
    		
    		
    int main()
    {
    	double a,b,c,gamma;
    	double laenge, alpha, beta;
    	
    	printf("Bitte gib a, b und gamma eines Dreiecks ein\n");
    	scanf("%lf %lf %lf",&a,&b,&gamma);
    	laenge = berechnung_length(a,b,gamma, c);
    	alpha = berechnung_alpha(a,b,c,gamma, alpha);
    	printf("a = %lf\t b = %lf\t gamma = %lf \t",a,b,gamma);
    	printf("Daraus folgt c = %lf, alpha = %lf und beta = %lf", laenge, alpha, beta);
    	return 0;
    	
    	
    }
    so i changed it up a bit, left my old weird stuff in with the //'s
    seems to kinda be working now, but idk if its my fault but it keeps returning -1.#IND00 or 0.00000 as the calculation
    *edit the c calculation is working
    Last edited by Lyrockzz; 11-21-2018 at 11:05 AM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Don't use function parameters as local variables!
    And you never call berechnung_beta.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
     
    #define TOTAL_DEG 180.0
     
    double berechnung_length(double a, double b, double gamma)
    {   
        return sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
    }
      
    double berechnung_alpha(double a, double b, double c, double gamma)
    {
        return gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));;
    }
     
    double berechnung_beta(double gamma, double alpha)
    {
        return -gamma - alpha + TOTAL_DEG;
    }
     
    int main()
    {
        double a, b, gamma;
        printf("Bitte gib a, b und gamma eines Dreiecks ein\n");
        scanf("%lf %lf %lf",&a,&b,&gamma);
     
        double laenge = berechnung_length(a,b,gamma);
        double alpha = berechnung_alpha(a,b,laenge,gamma);
        double beta = berechnung_beta(gamma, alpha);
     
        printf("a = %lf\t b = %lf\t gamma = %lf\n",a,b,gamma);
        printf("Daraus folgt c = %lf, alpha = %lf und beta = %lf\n",
            laenge, alpha, beta);
    }
    If it still gives wrong answers, you need to tell us what inputs you are using and what output you expected.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    What do you want to calculate? Side A, B, C or the degrees alpha, beta, gamma. Two sides and one angle or something. - like in the example below?

    Rechner zum Dreieck - Seiten, Höhe, Winkel, Flächeninhalt berechnen

    First time using pointers, and i feel lost :|-dreieck-berechnen-jpg

  7. #7
    Registered User
    Join Date
    Nov 2018
    Posts
    11
    im trying to calculate c, alpha and beta, the user should put in a,b and gamma

  8. #8
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by Lyrockzz View Post
    im trying to calculate c, alpha and beta, the user should put in a,b and gamma
    The calculation of side C, or the third side of a triangle, or the calculation of the angles alpha and beta requires different calculation types.
    If you want to calculate side C, the side whose length you are looking for (also A + B), then here is an example:

    Code:
    . . .
    #define PI 3.14159
    
    
    //Deklaration
    double gradmass_in_bogenmass(double winkelgrad);
    double dreieck_dritte_seite(double a, double b, double radiant);
    . . .
    
    
    //Convert in radiant
    double gradmass_in_bogenmass(double winkelgrad)
    {
        double radiant;
        
        radiant = (((2 * PI) / 360) * winkelgrad);
        return radiant;
    }
    
    
    //The side the being searched - Die Seite die gesucht wird
    double dreieck_dritte_seite(double a, double b, double radiant)
    {
        double y = 2.0;  //Hilfsvariable for pow()
        double seite_c;
            
        seite_c = sqrt((pow(a, y) + pow(b, y)) - (2 * a * b * cos(radiant)));
        return seite_c;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lost in structures and pointers
    By shallowbay in forum C Programming
    Replies: 7
    Last Post: 05-30-2013, 11:43 PM
  2. I feel like a stupid, pointers:
    By blunt in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2012, 11:08 AM
  3. hel plz im lost on pointers
    By padizzel in forum C Programming
    Replies: 3
    Last Post: 02-28-2011, 12:05 AM
  4. A little lost and confused with my pointers :/
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 6
    Last Post: 05-01-2009, 01:32 PM
  5. lost time
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-21-2001, 10:05 AM

Tags for this Thread